package handler

import (
	"context"
	"log"
	"math/rand"
	"ser-server/basic/config"
	"ser-server/internal/model"
	"ser-server/proto/user"
	"time"
)

type UserServer struct {
	user.UnimplementedUserServer
}

func (v *UserServer) SendSms(_ context.Context, req *user.SendSmsReq) (*user.SendSmsResp, error) {
	code := rand.Intn(9000) + 1000
	config.RDB.Set(context.Background(), "sendSms"+req.Mobile+req.Sours, code, time.Minute*3)
	return &user.SendSmsResp{}, nil
}
func (v *UserServer) SmsLogin(_ context.Context, req *user.SmsLoginReq) (*user.SmsLoginResp, error) {
	err := config.RDB.Get(context.Background(), "sendSms"+req.Mobile+req.Sours).Err()
	if err != nil {
		log.Println("验证错误")
	}
	var users model.User
	config.DB.Where("model = ?", req.Mobile).Find(&users)
	if req.Mobile == "" {
		log.Println("手机号为空,请重试")
	}
	if req.Password != users.Password {
		log.Println("用户密码错误，请重试")
	}
	newUser := model.User{
		Mobile:   req.Mobile,
		Password: req.Password,
	}
	config.DB.Create(&newUser)
	return &user.SmsLoginResp{
		Id: newUser.Id,
	}, nil
}
